import { Redirect, Route, Switch } from "react-router-dom";
import  { useState } from 'react';
import { getStore } from "../utils/localStore";
import { commonUtils } from "../utils/utils";


export interface IRouteViewProps {
    path?: string;
    redirect?: string;
    component?: any;
    exact? :boolean ;
    children?: IRouteViewProps[];
  }


  const RouteView = (props: IRouteViewProps) => {
    


    const [isLogin, setIsLogin] = useState(getStore(commonUtils.accessToken)!=null?true:false);




    return (
      <Switch>
        {props.children &&
          props.children.map((item, index) => {
            if (item.redirect) {
              return (
                <Redirect
                  key={index}
                  from={item.path}
                  to={item.redirect}
                ></Redirect>
              );
            }
            return (
              <Route
                key={index}
                path={item.path}
                exact={item.exact}
                render={(props) => {
                  return (
                    item.component && (
                      <item.component
                        children={item.children}
                        {...props}
                      ></item.component>
                    )
                  );
                }}
              ></Route>
            );
          })}
      </Switch>
    );
  };
  
  export default RouteView;
  